package com.integrate.multiple.sources.controller;

import com.integrate.multiple.sources.domain.consumer.ConsumerUser;
import com.integrate.multiple.sources.domain.producer.ProducerUser;
import com.integrate.multiple.sources.service.ConsumerUserService;
import com.integrate.multiple.sources.service.ProducerAndConsumerService;
import com.integrate.multiple.sources.service.ProducerUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 刘志强
 * @date 2020/11/27 14:18
 */
@RestController
@Slf4j
@CrossOrigin
public class TestController {

    @Autowired
    private ConsumerUserService consumerUserService;
    @Autowired
    private ProducerUserService producerUserService;
    @Autowired
    private ProducerAndConsumerService producerAndConsumerService;


    /**
     * 获取Consumer来源的用户
     * @param id
     * @return
     */
    @GetMapping("getConsumerUser")
    public ConsumerUser getConsumerUser(Long id) {
        return consumerUserService.getConsumerUser(id);
    }

    /**
     * 获取Producer来源的用户
     * @param id
     * @return
     */
    @GetMapping("getProducerUser")
    public ProducerUser getProducerUser(Long id) {
        return producerUserService.getProducerUser(id);
    }

    /**
     * 获取Consumer来源和Producer来源的用户
     * @param producerId
     * @param consumerId
     * @return
     */
    @GetMapping("getProducerUserAndConsumerUser")
    public Map<String,Object> getProducerUserAndConsumerUser(Long producerId, Long consumerId) {
        return producerAndConsumerService.getProducerUserAndConsumerUser(producerId,consumerId);
    }

    /**
     * 添加consumer用户
     * @param consumerUser
     * @return
     */
    @PostMapping("addConsumerUser")
    public String addConsumerUser(ConsumerUser consumerUser) {
        return consumerUserService.addConsumerUser(consumerUser);
    }

    /**
     * 添加producer用户
     * @param producerUser
     * @return
     */
    @PostMapping("addProducerUser")
    public String addProducerUser(ProducerUser producerUser) {
        return producerUserService.addProducerUser(producerUser);
    }

    /**
     * 添加producer和consumer用户
     * 会产生分布式事务问题
     * @param producerUserName
     * @param consumerUserName
     * @return
     */
    @PostMapping("addProducerUserAndConsumerUser")
    public String addProducerUserAndConsumerUser(String producerUserName, String consumerUserName) {
        return producerAndConsumerService.addProducerUserAndConsumerUser(producerUserName,consumerUserName);
    }
}
